package com.bit.cfdi.web.services;

import java.util.Calendar;
import java.util.HashMap;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bit.cfdi.web.dao.ComprobanteConceptoDAO;
import com.bit.cfdi.web.dao.UtilDAO;
import com.bit.cfdi.web.domain.ComprobanteConcepto;
@Service
public class ComprobanteConceptoService {

	@Autowired
	private ComprobanteConceptoDAO comprobanteConceptoDAO;

	@Autowired
	private UtilDAO utilDAO;

	@Resource(name = "messageSource")
	private MessageSource messageSource;

	public HashMap<String, Double> getImportePorConceptoFecha(
			Long idConcepto, Calendar fechaInicio, Calendar fechaFin, int tipo) throws DataAccessException { /*
		HashMap<String, Double> comprobanteConceptoMap = null;
		List<ComprobanteConcepto> comprobanteConceptoList = null;
		String key = null;
		SimpleDateFormat sfd = new SimpleDateFormat(SAFEConstants.DATE_FORMAT_DD_MM_YYYY);

		if (tipo == SAFEConstants.DIAGRAMA_HORAS) {
			comprobanteConceptoMap = new HashMap<String, Double>();
			comprobanteConceptoList = comprobanteConceptoDAO.getImportePorConceptoFecha(idConcepto, fechaInicio, fechaFin);

			if (null != comprobanteConceptoList)
				for (ComprobanteConcepto comprobanteConcepto : comprobanteConceptoList) {
					Calendar time=null!=comprobanteConcepto.getComprobante().getFechaTimbrado()?comprobanteConcepto.getComprobante().getFechaTimbrado()
							:comprobanteConcepto.getComprobante().getFechaCreacion();
					key = String.valueOf(time.get(Calendar.HOUR_OF_DAY));
					double totalSuma = comprobanteConcepto.getImporte() * (null!=comprobanteConcepto.getComprobante().getTipoDeCambio()?comprobanteConcepto.getComprobante().getTipoDeCambio():1);

					if (null != comprobanteConceptoMap.get(key)) {
						totalSuma = ((Double) comprobanteConceptoMap.get(key)).doubleValue();
						//DOLLAREMM
						totalSuma += comprobanteConcepto.getImporte() * (null!=comprobanteConcepto.getComprobante().getTipoDeCambio()?comprobanteConcepto.getComprobante().getTipoDeCambio():1);
						comprobanteConceptoMap.remove(key);
					}

					comprobanteConceptoMap.put(key, totalSuma);
				}
		} else if (tipo == SAFEConstants.DIAGRAMA_DIAS) {
			comprobanteConceptoMap = new HashMap<String, Double>();
			comprobanteConceptoList = comprobanteConceptoDAO.getImportePorConceptoFecha(idConcepto, fechaInicio, fechaFin);

			if (null != comprobanteConceptoList)
				for (ComprobanteConcepto comprobanteConcepto : comprobanteConceptoList) {
					Calendar time = null!=comprobanteConcepto.getComprobante().getFechaTimbrado()?comprobanteConcepto.getComprobante().getFechaTimbrado()
							:comprobanteConcepto.getComprobante().getFechaCreacion();
					int dayOfWeek = time.get(Calendar.DAY_OF_WEEK);

					if (dayOfWeek == Calendar.SUNDAY)
						key = "Domingo " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.MONDAY)
						key = "Lunes " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.TUESDAY)
						key = "Martes " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.WEDNESDAY)
						key = "Miercoles " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.THURSDAY)
						key = "Jueves " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.FRIDAY)
						key = "Viernes " + sfd.format(time.getTime());
					else if (dayOfWeek == Calendar.SATURDAY) {
						key = "Sabado " + sfd.format(time.getTime());
					}

					double totalSuma = comprobanteConcepto.getImporte() * (null!=comprobanteConcepto.getComprobante().getTipoDeCambio()?comprobanteConcepto.getComprobante().getTipoDeCambio():1);

					if (null != comprobanteConceptoMap.get(key)) {
						totalSuma = ((Double) comprobanteConceptoMap.get(key)).doubleValue();
						//DOLLAREMM
						totalSuma += comprobanteConcepto.getImporte() * (null!=comprobanteConcepto.getComprobante().getTipoDeCambio()?comprobanteConcepto.getComprobante().getTipoDeCambio():1);
						comprobanteConceptoMap.remove(key);
					}

					comprobanteConceptoMap.put(key, totalSuma);
				}
		} else if (tipo == SAFEConstants.DIAGRAMA_SEMANA) {
			comprobanteConceptoMap = new HashMap<String, Double>();
			Calendar endWeek = Calendar.getInstance();
			endWeek.setTime(fechaInicio.getTime());
			Calendar startWeek = Calendar.getInstance();
			startWeek.setTime(fechaInicio.getTime());
			int weekNumber = 1;

			while (fechaFin.compareTo(endWeek) > 0) {
				endWeek.add(Calendar.DATE, 6);

				setTime(startWeek, 0, 0, 0);
				setTime(endWeek, 23, 59, 59);

				key = "Semana " + String.valueOf(weekNumber);
				double totalSuma = comprobanteConceptoDAO.getSumImportPorCconceptoFecha(idConcepto, startWeek, endWeek);

				comprobanteConceptoMap.put(key, totalSuma);

				endWeek.add(Calendar.DATE, 1);
				startWeek.setTime(endWeek.getTime());

				weekNumber++;
			}
		} else if (tipo == SAFEConstants.DIAGRAMA_MES) {
			comprobanteConceptoMap = new HashMap<String, Double>();
			Calendar endDate = Calendar.getInstance();
			endDate.setTime(fechaInicio.getTime());
			Calendar startDate = Calendar.getInstance();
			startDate.setTime(fechaInicio.getTime());

			int start = Integer.parseInt(String.valueOf(fechaInicio.get(Calendar.YEAR))
					+ (fechaInicio.get(Calendar.MONTH) < 10 ? "0" + String.valueOf(fechaInicio.get(Calendar.MONTH)) : String
							.valueOf(fechaInicio.get(Calendar.MONTH))));
			int end = Integer.parseInt(String.valueOf(fechaFin.get(Calendar.YEAR))
					+ (fechaFin.get(Calendar.MONTH) < 10 ? "0" + String.valueOf(fechaFin.get(Calendar.MONTH)) : String
							.valueOf(fechaFin.get(Calendar.MONTH))));

			while (start <= end) {
				int dayMonth = startDate.getActualMinimum(Calendar.DAY_OF_MONTH);
				int daysMonth = startDate.getActualMaximum(Calendar.DAY_OF_MONTH);

				startDate.set(Calendar.DATE, dayMonth);
				endDate.set(Calendar.DATE, daysMonth);

				setTime(startDate, 0, 0, 0);
				setTime(endDate, 23, 59, 59);

				key = String.valueOf(startDate.get(Calendar.YEAR)) + " / "
						+ String.valueOf(SAFEUtils.formatString00(startDate.get(Calendar.MONTH) + 1));
				double totalSuma = comprobanteConceptoDAO.getSumImportPorCconceptoFecha(idConcepto, startDate, endDate);

				comprobanteConceptoMap.put(key, totalSuma);

				startDate.add(Calendar.MONTH, 1);
				endDate.add(Calendar.MONTH, 1);
				start = Integer.parseInt(String.valueOf(startDate.get(Calendar.YEAR))
						+ (startDate.get(Calendar.MONTH) < 10 ? "0" + String.valueOf(startDate.get(Calendar.MONTH)) : String
								.valueOf(startDate.get(Calendar.MONTH))));
			}
		}

		return comprobanteConceptoMap;*/
		return null;
	}

	public void saveChartConcept(Object chart, String title, Long idConcepto, int type) throws DataAccessException {/*
		JFreeChart graph = (JFreeChart) chart;

		String header = "", footer1 = "", footer2 = "", reportPath = "";
		String urlJar = System.getProperty("user.dir");
		JasperReport report;

		try {
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("imageReport", graph.createBufferedImage(500, 257));
			parameters.put("idCliente", idConcepto);
			parameters.put("title", title);
			parameters.put("type", type);
			header = messageSource.getMessage("cfdi.report.image.header.lin", null, LocationLanguage.getLocation());
			footer1 = messageSource.getMessage("cfdi.report.image.footer1.lin", null, LocationLanguage.getLocation());
			footer2 = messageSource.getMessage("cfdi.report.image.footer2.lin", null, LocationLanguage.getLocation());
			reportPath = messageSource.getMessage("path.report.cliente.lin", null, LocationLanguage.getLocation());
			parameters.put("headerReport", urlJar + header);
			parameters.put("footer1", urlJar + footer1);
			parameters.put("footer2", urlJar + footer2);
			report = JasperCompileManager.compileReport(urlJar + reportPath);
			JasperPrint print = JasperFillManager.fillReport(report, parameters);
			JRViewer vw = new JRViewer(print, LocationLanguage.getLocation());
			JRSaveContributor[] saveTypeFile = vw.getSaveContributors();
			JRSaveContributor[] saveContribs = new JRSaveContributor[3];
			int i = 0;
			for (JRSaveContributor typeFile : saveTypeFile) {
				String tipo = typeFile.getDescription();
				if (tipo.contains("pdf") || tipo.contains("xls")) {
					saveContribs[i] = typeFile;
					i++;
				}
			}
			vw.setSaveContributors(saveContribs);

			URL url = getClass().getResource(messageSource.getMessage("img.icon.cfdiapp", null, LocationLanguage.getLocation()));
			JFrame j = new JFrame();
			j.getContentPane().add(vw, java.awt.BorderLayout.CENTER);
			j.setIconImage(new ImageIcon(url).getImage());
			j.setTitle(messageSource.getMessage("msg.title.report", null, LocationLanguage.getLocation()));

			j.pack();

			Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
			java.awt.Dimension screenSize = toolkit.getScreenSize();
			int screenResolution = toolkit.getScreenResolution();
			float zoom = ((float) screenResolution) / 72;

			int height = (int) (550 * zoom);
			if (height > screenSize.getHeight()) {
				height = (int) screenSize.getHeight();
			}
			int width = (int) (750 * zoom);
			if (width > screenSize.getWidth()) {
				width = (int) screenSize.getWidth();
			}

			java.awt.Dimension dimension = new java.awt.Dimension(width, height);
			j.setSize(dimension);
			j.setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);

			j.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
	}

	public void setTime(Calendar date, int hour, int min, int sec) {
		date.set(Calendar.HOUR, hour);
		date.set(Calendar.MINUTE, min);
		date.set(Calendar.SECOND, sec);
		date.set(Calendar.HOUR_OF_DAY, hour);
		date.set(Calendar.MILLISECOND, 0);
	}

	public void save(final ComprobanteConcepto comprobanteConcepto) throws DataAccessException {
		comprobanteConceptoDAO.save(comprobanteConcepto);
	}

}
